//
//  EventSettingself.m
//  01-Event
//
//  Created by apple on 13-12-23.
//  Copyright (c) 2013年 Event. All rights reserved.
//
#define FONT(font)  [UIFont systemFontOfSize:font]


#define headerView_margin_top 0
#define hearderView_leftButton_width 40
#define hearderView_leftButton_x 10
#define hearderView_leftButton_height 74
#define tabelView_row_height 44
#define keyboard_height 216
#define tabelView_Foot_height 20
#define navigationBarLetf_width 60
#define navigationBarLetf_height 40
#define label_font 13
#define labelText_font 15
#define tabelViewCell_UIImageView_width 15




#import "EventSettingCell.h"
#import "EventSettingItem.h"

#import "UIView+Extension.h"
#import "EventContentView.h"
#import "EventTimeView.h"


typedef void(^callBack)();

@interface EventSettingCell()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic, strong) UIImageView *arrow;
@property (nonatomic, strong) UISwitch *switchON;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIImageView *imageIcon;

@property (nonatomic, strong) UIView *lineView;

//内容视图
@property (nonatomic, strong) EventContentView *titleView;

//时间
@property (nonatomic, strong) EventTimeView *timeView;


@property (nonatomic, weak) UIView *sepView;
@property CGFloat keyboardHeightOffset;
@property (strong,nonatomic) UITapGestureRecognizer *tapGes;


@property (nonatomic, assign) BOOL textfieldEdit;

@property (nonatomic, assign) BOOL textViewEdit;

@end

@implementation EventSettingCell

+ (id)settingCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"Cell";

    EventSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {
        cell = [[EventSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.imageView.width = tabelViewCell_UIImageView_width;
    self.imageView.height  = tabelViewCell_UIImageView_width;
    self.imageView.contentMode =UIViewContentModeScaleAspectFit;
    self.imageView.x  = tabelViewCell_UIImageView_width;
    self.imageView.y  = tabelViewCell_UIImageView_width + 1;

}

- (void)setItem:(EventSettingItem *)item
{
    _item = item;
    //设置数据
    self.textLabel.text = item.title;
    if (item.titleFont) {
          self.textLabel.font = FONT(item.titleFont);
    }else{
         self.textLabel.font = FONT(labelText_font);
    }
   
    self.textLabel.textColor = item.textColor;
    self.detailTextLabel.text = item.desc;
    self.detailTextLabel.textColor = item.detailLabelColor;
    self.detailTextLabel.font = FONT(label_font);
    self.imageView.image = item.icon;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    if (item.type == EventSettingItemTypeArrow) {
        
         [self.contentView addSubview:self.lineView];
        
        _arrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
        _arrow.image = [UIImage imageNamed:@"icon_22"];
        _arrow.contentMode = UIViewContentModeScaleAspectFit;
        // 右边显示箭头
        self.accessoryView = _arrow;
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        // 用默认的选中样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
     
    } else if (item.type == EventSettingItemTypeSwitch) {
         [self.contentView addSubview:self.lineView];
        self.switchON.on = item.isSwitchON;
        self.switchON.tag = item.scheduleType;
        
        self.accessoryView = self.switchON;
        self.accessoryType = UITableViewCellAccessoryNone;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    } else if (item.type == EventSettingItemTypeSelectArrow) {
         [self.contentView addSubview:self.lineView];
        _arrow = [[UIImageView alloc] initWithFrame:CGRectMake(KScreenWidth, 15, 15, 15)];
        
        if (self.item.isArrowHidden) {
            _arrow.image = nil;
        }else{
            _arrow.image = [UIImage imageNamed:item.arrowImageName];
        }
      
        // 右边显示箭头
        self.accessoryView = _arrow;
        
        // 用默认的选中样式
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    } else if (item.type == EventSettingItemTypeTextField) { //单行文本
         [self.contentView addSubview:self.lineView];
        //设置文字属性
        self.textField.text = item.text;
        self.textField.placeholder = item.placeHolder;
        self.textField.tag = item.scheduleType;
        self.accessoryView = self.textField;
        self.accessoryType = UITableViewCellAccessoryNone;
        // 禁止选中
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    } else if (item.type == EventSettingItemTypeImage) {
        
        if (_textField == nil) {
            _imageIcon = [[UIImageView alloc] init];
            _imageIcon.image = item.image;
            _imageIcon.width = 100;
        }
        
        // 右边显示开关
        self.accessoryView = _imageIcon;
        self.accessoryType = UITableViewCellAccessoryNone;

        // 禁止选中
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }else if(item.type == EventSettingItemTypeTextView)  {
        
        self.imageView.frame = CGRectMake(10, 10, 20, 20);
//        UIImageView *iv = [[UIImageView alloc] init];
//        iv.contentMode = UIViewContentModeScaleAspectFit;
//        iv.frame =  CGRectMake(10, 10, 20, 20);
//        iv.image =item.icon;
//        [self.contentView addSubview:iv];
//        
//        self.textView.placeholder = item.placeHolder;
//        self.textView.tag = item.scheduleType;
//        if (!ZYIsNullOrEmpty(item.text)) {
//            self.textView.text = item.text;
//        }
//        self.accessoryView = self.textView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
         self.accessoryType = UITableViewCellAccessoryNone;
        
    }else if(item.type == EventSettingItemTypeBtnAndTextView)  {
        
        CGRect frame = CGRectMake(15, 0, KScreenWidth - 30, 170);
        self.titleView.tag = item.scheduleType;
        
        self.titleView = [[EventContentView alloc] initWithFrame:frame textArr:item.titleArr btnColorArr:item.colorArr palcerHoler:item.placeHolder text:item.text tag:item.scheduleType selectIndex:item.selectedIndex];
//        
////        if (!ZYIsNullOrEmpty(item.text)) {
//            self.textView.text = item.text;
////        }
        
            self.accessoryView = self.titleView;
            self.selectionStyle = UITableViewCellSelectionStyleNone;
            self.accessoryType = UITableViewCellAccessoryNone;
        
        
    }else if(item.type == EventSettingItemTypeBtnAndLabel)  { //开始时间和结束时间
        
        CGRect frame = CGRectMake(10, 0, KScreenWidth, tabelView_row_height);
        self.timeView.tag = item.scheduleType;
        self.timeView = [[EventTimeView alloc] initWithFrame:frame startTime:item.startTime endTime:item.endTime];
      
        self.accessoryView = self.timeView;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        
    }else{
        
         [self.contentView addSubview:self.lineView];
        
         self.textLabel.font = FONT(15);
         self.textLabel.numberOfLines = 0;
         self.accessoryView = nil;
         self.selectionStyle = UITableViewCellSelectionStyleNone;
         self.accessoryType = UITableViewCellAccessoryNone;
    }
}

//创建文本
- (UITextField *)textField{
    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        _textField.font = FONT(label_font);
        _textField.frame = CGRectMake(0, 0, KScreenWidth - 50, tabelView_row_height);
        _textField.delegate = self;
        _textField.textAlignment = NSTextAlignmentLeft;
        
    }
    return _textField;
}

- (UISwitch *)switchON{
    if (_switchON == nil) {
        _switchON = [[UISwitch alloc] init];
        [_switchON addTarget:self action:@selector(settingSwitchClick:) forControlEvents:UIControlEventValueChanged];
    }
    return _switchON;
}


- (UIView *)lineView{
    if (_lineView == nil) {
       _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = XRGB(d9, d9, d9);
        _lineView.frame =CGRectMake(0, self.height - 0.5, KScreenWidth, 0.5);
    }
    
    return _lineView;
}


- (void)settingSwitchClick:(UISwitch *)switchOn
{
    //点击开关发送通知
    NSString *switchStatus =[NSString stringWithFormat:@"%d",switchOn.on];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:switchStatus,@"switchStatus", nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"clickSwitchStatus" object:nil userInfo:dict];

}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
}

- (void)textFieldDidEndEditing:(UITextField *)textField{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"textFieldChange" object:nil];

    
//    [textField resignFirstResponder];
//    [textField endEditing:YES];

}

- (void)textViewDidBeginEditing:(UITextView *)textView{
     if (textView.tag == buildScheduleContent) return;
    
    
    }

- (void)textViewDidEndEditing:(UITextView *)textView{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"textViewChange" object:nil];
    
    if (textView.tag == buildScheduleContent) return;
    

}


- (void)setBtnCallBack:(BtnCallBack)btnCallBack{
    _btnCallBack = btnCallBack;
    __weak typeof (self) weakSelf = self;
    if (btnCallBack) {
        weakSelf.titleView.btnBlock = ^(NSInteger index){
            btnCallBack(index);
        };
    }
}

- (void)setBtnTimeBlock:(BtnTimeBlock)btnTimeBlock{
    _btnTimeBlock = btnTimeBlock;
    __weak typeof (self) weakSelf = self;
    if (btnTimeBlock) {
        weakSelf.timeView.btnTimeBlock = ^(NSInteger index){
            btnTimeBlock(index);
        };
    }

}




@end
